package com.flyme.module.base.provider.service.impl;

import cn.hutool.core.date.DateUtil;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.common.core.utils.ObjectUtils;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.core.base.supper.service.impl.BaseServiceImpl;
import com.flyme.module.base.client.entity.SysPushCourse;
import com.flyme.module.base.client.entity.SysUser;
import com.flyme.module.base.provider.mapper.SysPushCourseMapper;
import com.flyme.module.base.provider.service.SysPushCourseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author: zyf
 * @date: 2018/10/24 16:33
 * @description:
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysPushCourseServiceImpl extends BaseServiceImpl<SysPushCourseMapper, SysPushCourse> implements SysPushCourseService {

    /**
     * 分页查询用户计划
     */
    public List<EntityMap> selectByUser(SysUser user, PagerInfo pagerInfo) {
        pagerInfo.setPageSize(10);
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
        queryWrapper.select("p.id,p.createTime,p.targetType,c.courseTitle,c.id courseId,c.courseImage," +
            "(select count(id) from sys_catalog ca where ca.courseId=c.id and parentId!=0) courseCount");
        if (ObjectUtils.isNotEmpty(user.getCompanyId())) {
            queryWrapper.eq("p.companyId", user.getCompanyId());
            //queryWrapper.or();
        }
        queryWrapper.in("targetId", user.getId(), user.getDeptId());
        queryWrapper.orderByDesc("createTime");
        queryWrapper.createAlias("course", "c");
        queryWrapper.groupBy("courseId");
        return (List<EntityMap>) pageList(queryWrapper).getRows();
    }


    /**
     * 网站分页查询用户计划
     */
    public PqGrid selectByUserWeb(SysUser user, PagerInfo pagerInfo) {
        pagerInfo.setPageSize(10);
        /*CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
        queryWrapper.select("p.id,p.createTime,p.targetType,c.courseTitle,c.id courseId,c.courseImage,c.courseCount");
        queryWrapper.eq("p.id", "1");
        queryWrapper.or();
        queryWrapper.eq("targetId", user.getId());
        queryWrapper.or();
        queryWrapper.eq("targetId", user.getDepartmentId());
        queryWrapper.orderByDesc("createTime");
        queryWrapper.createAlias("course", "c");
        queryWrapper.groupBy("courseId");*/
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
//        queryWrapper.select("p.id,p.createTime,p.targetType,c.courseTitle,c.id courseId,c.courseImage,c.courseCount");
        queryWrapper.select("p.id,p.createTime,p.targetType,c.courseTitle,c.id courseId,c.courseImage," +
            "(select count(id) from sys_catalog ca where ca.courseId=c.id and parentId!=0) courseCount");
        if (ObjectUtils.isNotEmpty(user.getCompanyId())) {
            queryWrapper.eq("p.companyId", user.getCompanyId());
            //queryWrapper.or();
        }
        queryWrapper.in("targetId", user.getId(), user.getDeptId());
        queryWrapper.orderByDesc("createTime");
        queryWrapper.createAlias("course", "c");
        queryWrapper.groupBy("courseId");
        return pageList(queryWrapper);
    }

    /**
     * 查询用户该课程的学习计划
     */
    public PqGrid selectUserPlan(Long companyId, Long[] ids, Long courseId) {
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(new PagerInfo());
        queryWrapper.select("p.*,c.starTime,c.endTime");
        queryWrapper.eq("p.companyId", companyId);
        queryWrapper.in("p.targetId", ids);
        queryWrapper.in("c.courseIds", courseId);
        queryWrapper.le("c.starTime", DateUtil.now());
        queryWrapper.ge("c.endTime", DateUtil.now());
        queryWrapper.groupBy("c.id");
        queryWrapper.createAlias("complan", "c");
        return pageList(queryWrapper);
    }
}
